#!/usr/bin/env python
#
#Copyright 2011 Velin Georgiev
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

''' The datamodel.py implements the database of the blog and some related to the tables transactions '''

__author__ = 'Velin Georgiev'

from google.appengine.api import memcache, users
from google.appengine.ext import db, webapp
import re

class tbl_posts(db.Model):
	title = db.StringProperty()
	content = db.TextProperty()
	category = db.StringProperty()
	visible = db.BooleanProperty()
	author = db.UserProperty()
	modified_by = db.UserProperty()
	date = db.DateTimeProperty(auto_now_add=True)
	
	def new_item(e, strTitle, strContent, strCategory=None, bVisible=True):
		#TRAN INSERT INTO tbl_posts
		if not users.get_current_user():return
		strUser = users.get_current_user()
		obj = tbl_posts(
			title = unicode(strTitle),
			content = unicode(strContent),
			category = unicode(strCategory),
			visible = bVisible,
			author = strUser,
			modified_by = strUser
		)
		obj.put()
	
	def update_item(e, ID, strTitle, strContent, strCategory=None, bVisible=False):
		#TRAN UPDATE tbl_posts
		if not users.get_current_user():return
		obj = tbl_posts.get_by_id(long(ID))
		obj.title = unicode(strTitle)
		obj.content = unicode(strContent)
		obj.category = unicode(strCategory)
		obj.visible = bVisible
		obj.modified_by = users.get_current_user()
		obj.put()
		
	def delete_item(e, ID):
		#TRAN DELETE FROM tbl_posts
		if not users.get_current_user():return
		obj = tbl_posts.get_by_id(long(ID))
		obj.delete()
		
	def select_distinct_category(e):
		#TRAN SELECT DISTINCT FROM tbl_posts
		#if not users.get_current_user():return
		query = tbl_posts.all()
		unique_results = []
		for obj in query:
			if obj.category not in unique_results:
				unique_results.append(obj.category)
		return unique_results
		
	def search(e, strWord):
		query = tbl_posts.all().filter('visible =', True)
		results = {}	
		for obj in query:
			if re.search(strWord.lower(), str(obj.content).lower()):
				results[obj.title]=int(obj.key().id())
		return results
	
class tbl_comments(db.Model):
	post_id = db.IntegerProperty()
	comment = db.TextProperty()
	visible = db.BooleanProperty()
	author = db.UserProperty()
	modified_by = db.UserProperty()
	date = db.DateTimeProperty(auto_now_add=True)
	
	def new_item(e, ID, strComment, bVisible=True):
		#TRAN INSERT INTO tbl_comments
		if not users.get_current_user():return
		obj = tbl_comments(
			post_id = long(ID),
			comment = unicode(strComment),
			visible = bVisible,
			author = users.get_current_user(),
			modified_by = users.get_current_user()
		)
		obj.put()
	
	def update_item(e, ID, strComment, bVisible=True):
		#TRAN UPDATE tbl_comments
		if not users.get_current_user():return
		obj = tbl_comments.get_by_id(long(ID))
		obj.visible = bVisible
		obj.comment = unicode(strComment)
		obj.modified_by = users.get_current_user()
		obj.put()
		
	def delete_item(e, ID):
		#TRAN DELETE FROM tbl_comments
		if not users.get_current_user():return
		obj = tbl_comments.get_by_id(long(ID))
		obj.delete()
	
	def delete_all_related_items(e, ID):
		#TRAN DELETE FROM tbl_comments WHERE post_id match 
		if not users.get_current_user():return
		query = db.Query(tbl_comments).filter('post_id =', long(ID))
		for item in query:
			item.delete()
			
	def get_user_items(e):
		#TRAN SELECT 
		if not users.get_current_user():return
		query = db.Query(tbl_comments).filter('author =',users.get_current_user()).filter('visible =', True).order('-date')
		unique_results = []
		for obj in query:
			if obj.post_id not in unique_results:
				unique_results.append(obj.post_id)
		return unique_results
		
		